﻿using DemoTG;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TGCommunity;
using SolidEdgeFramework;
using SolidEdgeConstants;
using TGComacAddin.View;

namespace TGComacAddin
{


    public class TGSelect : SolidEdgeFramework.ISECommandEvents, SolidEdgeFramework.ISEMouseEvents, SolidEdgeFramework.ISELocateFilterEvents
    {
        protected SolidEdgeFramework.ISEMouseEx _mouse;
        private ConnectionPointController _connectController;
        protected SolidEdgeFramework.Command _cmd;

        /// <summary>
        /// 连接事件源
        /// </summary>
        private void ConnectEventSource()
        {
            _connectController.AdviseSink<SolidEdgeFramework.ISEMouseEvents>(_mouse);
            _connectController.AdviseSink<SolidEdgeFramework.ISECommandEvents>(_cmd);
            _connectController.AdviseSink<SolidEdgeFramework.ISELocateFilterEvents>(_mouse);
        }

        /// <summary>
        /// 断开事件源
        /// </summary>
        private void DisconnectEventSource()
        {
            _connectController.UnadviseAllSinks();
        }

        public TGSelect(ISEMouseEx mouse, SolidEdgeFramework.Command cmd)
        {
            _cmd = cmd;
            _mouse = mouse;
            _connectController = new ConnectionPointController(this);
            ConnectEventSource();
        }

        protected virtual void ConfigLocateFilter()
        {
            
            _mouse.AddToLocateFilter((int)seLocateFilterConstants.seLocateSketch3D);
        }


        void SolidEdgeFramework.ISECommandEvents.Activate()
        {
            _mouse.ScaleMode = 1;                // Design model coordinates.
            _mouse.WindowTypes = 1;              // Graphic window's only please.
            _mouse.EnabledDrag = true;
            _mouse.EnabledMove = true;   // Request move events.
            _mouse.LocateMode = (int)seLocateModes.seLocateSimple;
            _mouse.ClearLocateFilter();
            ConfigFilter();
        }

        void SolidEdgeFramework.ISECommandEvents.Deactivate()
        {
            throw new NotImplementedException();
        }

        void SolidEdgeFramework.ISECommandEvents.Terminate()
        {
            DisconnectEventSource();
            throw new NotImplementedException();
        }

        void SolidEdgeFramework.ISECommandEvents.Idle(int lCount, out bool pbMore)
        {
            throw new NotImplementedException();
        }

        void SolidEdgeFramework.ISECommandEvents.KeyDown(ref short KeyCode, short Shift)
        {
            throw new NotImplementedException();
        }

        void SolidEdgeFramework.ISECommandEvents.KeyPress(ref short KeyAscii)
        {
            throw new NotImplementedException();
        }

        void SolidEdgeFramework.ISECommandEvents.KeyUp(ref short KeyCode, short Shift)
        {
            throw new NotImplementedException();
        }

        void ISEMouseEvents.MouseDown(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, int lKeyPointType, object pGraphicDispatch)
        {
            throw new NotImplementedException();
        }

        void ISEMouseEvents.MouseUp(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, int lKeyPointType, object pGraphicDispatch)
        {
            throw new NotImplementedException();
        }


        void ISEMouseEvents.MouseMove(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, int lKeyPointType, object pGraphicDispatch)
        {
            OnMouseMove(sButton, sShift, dX, dY, dZ, pWindowDispatch, lKeyPointType, pGraphicDispatch);
        }



        void ISEMouseEvents.MouseClick(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, int lKeyPointType, object pGraphicDispatch)
        {
            OnMouseClick(sButton, sShift, dX, dY, dZ, pWindowDispatch, lKeyPointType, pGraphicDispatch);
        }

        void ISEMouseEvents.MouseDblClick(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, int lKeyPointType, object pGraphicDispatch)
        {
            throw new NotImplementedException();
        }

        void ISEMouseEvents.MouseDrag(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, short DragState, int lKeyPointType, object pGraphicDispatch)
        {
            throw new NotImplementedException();
        }

        void ISELocateFilterEvents.Filter(object pGraphicDispatch, out bool vbValid)
        {
            OnFilter(pGraphicDispatch, out vbValid);
        }

        protected virtual    void OnFilter(object pGraphicDispatch, out bool vbValid)
        {
            vbValid = false;
        }

        protected virtual void OnMouseClick(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, int lKeyPointType, object pGraphicDispatch)
        {

        }

        protected virtual  void OnMouseMove(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, int lKeyPointType, object pGraphicDispatch)
        {

        }

        protected virtual void ConfigFilter()
        {
            _mouse.AddToLocateFilter((int)seLocateFilterConstants.seLocateSketch3D);
        }
    }

    public class TGShowSurfaceInfo : TGSelect
    {

        SolidEdgeFramework.Application _seApp;
        SolidEdgeFramework.SolidEdgeDocument _doc;
        TGEdgeBarView _view;
        public TGShowSurfaceInfo(ISEMouseEx mouse, Command cmd, SolidEdgeDocument doc,TGEdgeBarView view) : base(mouse, cmd)
        {
            _doc = doc;
            _seApp= doc.Application;
            _view = view;
        }

        protected override void OnFilter(object pGraphicDispatch, out bool vbValid)
        {
            SolidEdgeGeometry.Face pFace = pGraphicDispatch as SolidEdgeGeometry.Face;
            if (pFace != null)
            {
                vbValid = true;
            }
            else
            {
                _seApp.StatusBar = "请选择一个面";
                vbValid = false;
            }
        }

        protected override void OnMouseClick(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, int lKeyPointType, object pGraphicDispatch)
        {
            SolidEdgeGeometry.Face pFace = pGraphicDispatch as SolidEdgeGeometry.Face;
            if(null == pFace)
                { return; }

            // 根据ID取面信息
            if (null == _view.SurfaceDict)
                return;

            int faceID = pFace.ID;
            string infoStr = "";
            if(_view.SurfaceDict.ContainsKey(faceID))
            {
                SurfaceInfo faceInfo = _view.SurfaceDict[faceID];
                infoStr = string.Format("材质:{1}   数目:{2}", faceID, faceInfo.MaterialName, faceInfo.Num);
              

            }else
            {
                infoStr = "没有找到对应的面信息";
            }
            _view.Dispatcher.Invoke(new Action(() => {
                _view.tbInfo.Text = infoStr;
            }));
        }

        protected override void OnMouseMove(short sButton, short sShift, double dX, double dY, double dZ, object pWindowDispatch, int lKeyPointType, object pGraphicDispatch)
        {
           // _seApp.StatusBar = "请选择一个面";
        }

        protected override void ConfigFilter()
        {
            _mouse.AddToLocateFilter((int)seLocateFilterConstants.seLocateFace);
        }

    }
}
